
/**
 * 
 * @param {string} type sessionStorage、localStorage、sessionStorage
 * @returns 
 */
export function useStorage(type) {
    const setStorage = (key, value) => {
      window[type].setItem(
        key,
        (typeof value === "object" || Array.isArray(value)) ? JSON.stringify(value) : value
      );
    };
  
    const getStorage = (key) => {
      const value = window[type].getItem(key);
      try {
        return JSON.parse(value || "");
      } catch (error) {
        return value;
      }
    };
  
    const removeStorage = (key) => {
      window[type].removeItem(key);
    };
  
    const clear = () => {
      window[type].clear();
    };
  
    return {
      setStorage,
      getStorage,
      removeStorage,
      clear,
    };
  };
